package cn.wolfcode.wms.service.impl;

import cn.wolfcode.wms.domain.Brand;
import cn.wolfcode.wms.domain.Product;
import cn.wolfcode.wms.mapper.ProductMapper;
import cn.wolfcode.wms.page.PageResult;
import cn.wolfcode.wms.qo.QueryObject;
import cn.wolfcode.wms.service.IBrandService;
import cn.wolfcode.wms.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements IProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private IBrandService brandService;

    public List<Product> listAll() {

        return productMapper.selectAll();
    }

    public Product getById(Long id) {
        return productMapper.selectByPrimaryKey(id);
    }

    public void saveOrUpdate(Product product) {
        //查询到对应的品牌
        Brand brand = brandService.getById(product.getBrandId());
        product.setBrandName(brand.getName());
        if (product.getId() != null) {
            productMapper.updateByPrimaryKey(product);
        } else {
            productMapper.insert(product);
        }
    }

    public void delete(Long id) {
        productMapper.deleteByPrimaryKey(id);
    }

    public PageResult query(QueryObject qo) {
        int rows = productMapper.queryForCount(qo);
        if(rows==0){
            return PageResult.empty(qo.getPageSize());
        }
        List<?> result= productMapper.queryForList(qo);
        return new PageResult(rows, result,qo.getCurrentPage(),qo.getPageSize());
    }
}
